﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCS.XNA.Utils;

namespace Hopeless.Metaphysical.GUI
{
    public class HUD
    {
        public HUD(HopelessGame game)
        {
            _game = game;
        }

        private const string LOSE_MESSAGE = "NOOB";
        private const string WIN_MESSAGE = "YOU WIN (NOBODY CARES)";
        private const string PAUSED_MESSAGE = "PAUSED";

        private HopelessGame _game;
        private SpriteFont _hudFont;
        private SpriteFont _messageFont;
        private Vector2 _loseMessagePosition;
        private Vector2 _winMessagePosition;
        private Vector2 _pausedMessagePosition;

        public void Initialize(Rectangle space)
        {
            _hudFont = GameResources.Content.Load<SpriteFont>("Calibri");
            _messageFont = GameResources.Content.Load<SpriteFont>("Calibri Big");

            var loseSize = _messageFont.MeasureString(LOSE_MESSAGE);
            _loseMessagePosition = new Vector2(space.Width / 2f - loseSize.X / 2f, space.Height / 2f - loseSize.Y / 2f);

            var winSize = _messageFont.MeasureString(WIN_MESSAGE);
            _winMessagePosition = new Vector2(space.Width / 2f - winSize.X / 2f, space.Height / 2f - winSize.Y / 2f);

            var pausedSize = _messageFont.MeasureString(PAUSED_MESSAGE);
            _pausedMessagePosition = new Vector2(space.Width / 2f - pausedSize.X / 2f, space.Height / 2f - winSize.Y / 2f);
        }

        public void Draw(GameTime gameTime)
        {
            GameResources.SpriteBatch.Begin();

            var universe = HopelessGame.Universe;
            var hero = universe.Hero;
            if (hero == null)
                GameResources.SpriteBatch.DrawString(_messageFont, LOSE_MESSAGE, _loseMessagePosition, Color.Red);
            else if (universe.AllWavesFinished && universe.CurrentWaveFinished)
                GameResources.SpriteBatch.DrawString(_messageFont, WIN_MESSAGE, _winMessagePosition, Color.Green);
            else if (_game.Paused)
                GameResources.SpriteBatch.DrawString(_messageFont, PAUSED_MESSAGE, _pausedMessagePosition, Color.Black);

            var currentWave = universe.CurrentWave;
            GameResources.SpriteBatch.DrawString(_hudFont, string.Format("Wave {0} zombies: {1}/{2}", currentWave.Number, currentWave.TotalZombies - currentWave.DeadZombies.Count, currentWave.TotalZombies), new Vector2(10, 10), Color.Black);
            GameResources.SpriteBatch.DrawString(_hudFont, string.Format("Life: {0}/{1}", hero == null ? 0 : hero.Life, 100), new Vector2(10, 30), Color.Black);

            GameResources.SpriteBatch.End();
        }
    }
}
